clear
clear all
cap log close

log using "log-files/3_Table_F2.smcl", replace smcl

timer on 2

* Loading rdrobust_export, which is a program written by us to export results
* from rdrobust
cap program drop rdrobust_export
include "codes/auxiliary_programs/rdrobust_export.ado"

* Loading myround, which is a program written by us to round numbers
cap program drop myround
include "codes/auxiliary_programs/myround.ado"

*-----------*
* Table F.2 *
*-----------*
	* Cutoff 2, Table 1. Robustness checks (different poly. degrees, regu. term, etc.)
	*---------------------------------------------------------------------------------
	use "data/outputs/computo_verificadortrepdate_nomissings.dta", clear

	gen row = 1
	merge m:1 row using "data/outputs/RD_sortings_mediandraw.dta"
	drop if _merge == 2
	drop _merge row

	merge 1:1 NumMesa sorting using "data/outputs/randomdraws_rd.dta"
	drop if _merge == 2
	drop _merge sorting

	keep Pais-Nulos trep_date_verificador random_draw

	* Measures that DO USE Blancos and Nulos (sorted)
	gen total_so = CC + FPV + MTS + UCS + MAS + F + PDC + MNR + PANBOL + ///
		Blancos + Nulos
	*---*
	gen mshare_so = MAS / total_so
	gen cshare_so = CC / total_so
	sort trep_date_verificador random_draw
	gen cumsumtotal_so = sum(total_so)
	*---*
	egen maxcumsumtotal_so = max(cumsumtotal_so)
	gen pcs_so = cumsumtotal_so / maxcumsumtotal_so
	drop cumsumtotal_so maxcumsumtotal_so

	sum pcs if trep_date_verificador >= tc(20oct2019 19:40:57)
	local cutoff = r(min)

	* Generating a variable to cluster at the precinct level
	egen reci_id = group(Pais Dep Prov Muni Loc Reci)
		/* Note, in constructing the data set we made a correction
		such that recinto names are unique within localities. */

	* p=1, regularization term=1 (default)
	rdrobust mshare pcs, c(`cutoff') vce(cluster reci_id)
	rdrobust_export mshare pcs, ///
		c("`cutoff'") apprep("replace") file("outputs/generated/Table_F2") ///
		colstring("Cutoff `cutoff' - Time 10 20 2019 19 40 57 - p=1 - regterm=1") ///
		vce(cluster reci_id)

	* p=2, regularization term=1 (default)
	rdrobust mshare pcs, p(2) c(`cutoff') vce(cluster reci_id)
	rdrobust_export mshare pcs, ///
		c("`cutoff'") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff `cutoff' - Time 10 20 2019 19 40 57 - p=2 - regterm=1") ///
		vce(cluster reci_id) p(2)

	* p=3, regularization term=1 (default)
	rdrobust mshare pcs, p(3) c(`cutoff') vce(cluster reci_id)
	rdrobust_export mshare pcs, ///
		c("`cutoff'") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff `cutoff' - Time 10 20 2019 19 40 57 - p=3 - regterm=1") ///
		vce(cluster reci_id) p(3)

	*---*
	* p=1, regularization term=0
	rdrobust mshare pcs, c(`cutoff') vce(cluster reci_id) scaleregul(0)
	rdrobust_export mshare pcs, ///
		c("`cutoff'") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff `cutoff' - Time 10 20 2019 19 40 57 - p=1 - regterm=0") ///
		vce(cluster reci_id) reguterm("no")

	* p=2, regularization term=0
	rdrobust mshare pcs, p(2) c(`cutoff') vce(cluster reci_id) scaleregul(0)
	rdrobust_export mshare pcs, ///
		c("`cutoff'") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff `cutoff' - Time 10 20 2019 19 40 57 - p=2 - regterm=0") ///
		vce(cluster reci_id) reguterm("no") p(2)

	* p=3, regularization term=0
	rdrobust mshare pcs, p(3) c(`cutoff') vce(cluster reci_id) scaleregul(0)
	rdrobust_export mshare pcs, ///
		c("`cutoff'") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff `cutoff' - Time 10 20 2019 19 40 57 - p=3 - regterm=0") ///
		vce(cluster reci_id) p(3) reguterm("no")

	* Cutoff 1, Table 1. Robustness checks (different poly. degrees, regu. term, etc.)
	*---------------------------------------------------------------------------------
	use "data/outputs/computo_verificadortrepdate_nomissings.dta", clear

	gen row = 3
	merge m:1 row using "data/outputs/RD_sortings_mediandraw.dta"
	drop if _merge == 2
	drop _merge row

	merge 1:1 NumMesa sorting using "data/outputs/randomdraws_rd.dta"
	drop if _merge == 2
	drop _merge sorting

	keep Pais-Nulos trep_date_verificador random_draw

	* Measures that DO USE Blancos and Nulos (sorted)
	gen total_so = CC + FPV + MTS + UCS + MAS + F + PDC + MNR + PANBOL + Blancos + Nulos
	*---*
	gen mshare_so = MAS / total_so
	gen cshare_so = CC / total_so
	sort trep_date_verificador random_draw
	gen cumsumtotal_so = sum(total_so)
	*---*
	egen maxcumsumtotal_so = max(cumsumtotal_so)
	gen pcs_so = cumsumtotal_so / maxcumsumtotal_so
	drop cumsumtotal_so maxcumsumtotal_so

	* Extracting the date associated to the pcs cutoff of 0.95
	sum pcs if pcs >= 0.95
	local cutoff=r(min)

	gen hour = hh(trep_date) if pcs == `cutoff'
	gen min = mm(trep_date) if pcs == `cutoff'
	gen secs = ss(trep_date) if pcs == `cutoff'
	gen date = dofc(trep_date) if pcs == `cutoff'
	format date %td
	gen day = day(date) if pcs == `cutoff'
	gen month = month(date) if pcs == `cutoff'
	gen year = year(date) if pcs == `cutoff'

	global date ""
	foreach g in month day year hour min secs {
		quietly sum `g'
		local loc_`g' = r(mean)
		drop `g'
		global date "$date `loc_`g''"
	}
	drop date

	* Generating a variable to cluster at the precinct level
	egen reci_id = group(Pais Dep Prov Muni Loc Reci)
		/* Note, in constructing the data set we made a correction
		such that recinto names are unique within localities. */

	* p=1, regularization term=1 (default)
	rdrobust mshare pcs, c(0.95) vce(cluster reci_id)
	rdrobust_export mshare pcs, ///
		c("0.95") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff 0.95 - Time $date - p=1 - regterm=1") ///
		vce(cluster reci_id)

	* p=2, regularization term=1 (default)
	rdrobust mshare pcs, c(0.95) vce(cluster reci_id) p(2)
	rdrobust_export mshare pcs, ///
		c("0.95") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff 0.95 - Time $date - p=2 - regterm=1") ///
		vce(cluster reci_id) p(2)

	* p=2, regularization term=1 (default)
	rdrobust mshare pcs, c(0.95) vce(cluster reci_id) p(3)
	rdrobust_export mshare pcs, ///
		c("0.95") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff 0.95 - Time $date - p=3 - regterm=1") ///
		vce(cluster reci_id) p(3)

	*---*
	* p=1, regularization term=0
	rdrobust mshare pcs, c(0.95) vce(cluster reci_id) scaleregul(0)
	rdrobust_export mshare pcs, ///
		c("0.95") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff 0.95 - Time $date - p=1 - regterm=0") ///
		vce(cluster reci_id) reguterm("no")

	* p=2, regularization term=0
	rdrobust mshare pcs, c(0.95) vce(cluster reci_id) scaleregul(0) p(2)
	rdrobust_export mshare pcs, ///
		c("0.95") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff 0.95 - Time $date - p=2 - regterm=0") ///
		vce(cluster reci_id) reguterm("no") p(2)

	* p=3, regularization term=0
	rdrobust mshare pcs, c(0.95) vce(cluster reci_id) scaleregul(0) p(3)
	rdrobust_export mshare pcs, ///
		c("0.95") apprep("append") file("outputs/generated/Table_F2") ///
		colstring("Cutoff 0.95 - Time $date - p=3 - regterm=0") ///
		vce(cluster reci_id) reguterm("no") p(3)

	* Formatting the Table
	*---------------------
	use "outputs/generated/Table_F2.dta", clear

	drop obs band_r conv_*

	format obs_l obs_r %12.0fc

	foreach x of varlist estim - rob_ci_high {
		myround `x', decimals(3)
	}
	tostring obs_l obs_r, force replace format(%12.0fc)

	gen rob_ci = "[" + rob_ci_low + ", " + rob_ci_high + "]"
	drop rob_ci_low rob_ci_high

	split colstring, p("-")
	drop colstring

	rename colstring2 time
	replace time = subinstr(time, "Time ", "", .)
	split time, p(" ")
	split time6, p(".")
	drop time
	replace time5 = "0" + time5 if length(time5) == 1
	gen time = time1 + "/" + time2 + "/" + time3 + " " + time4 + ":" + ///
		time5 + ":" + time61
	drop time1 time2 time3 time4 time5 time6*

	rename colstring1 cutoff
	replace cutoff=subinstr(cutoff,"Cutoff","",.)
	destring cutoff, replace
	myround cutoff, decimals(3)

	rename colstring3 degpoly
	replace degpoly = subinstr(degpoly, "p=", "", .)

	rename colstring4 reguterm
	replace reguterm = subinstr(reguterm, "regterm=", "", .)

	gen t2 = _n
	gen t = 1 if time == "10/20/2019 20:03:59"
	replace t = 2 if time == "10/20/2019 19:40:57"
	sort t t2
	drop t t2

	order cutoff time reguterm degpoly estim band_l rob_pv rob_ci ///
		obs_l obs_r
	egen c = concat(cutoff time reguterm degpoly estim band_l rob_pv ///
		rob_ci obs_l obs_r), punct(" & ")
	keep c
	replace c = c + " \\"

	gen t = _n
	set obs 13

	replace c = "\hline" if c == ""
	replace t = 6.5 if _n == 13
	sort t
	drop t

	outsheet using "outputs/generated/Table_F2.tex", replace noname ///
		noquote nolabel
	erase "outputs/generated/Table_F2.dta"

*------------------------------------------------------------------------------*
timer off 2
timer list 2

log close
clear all
